<?php
    
    /* testando SVN */
class NodoController extends Zend_Controller_Action
    {
    
        public function preDispatch()
        {
            $registry = Zend_Registry::getInstance();
            $registry->set('tela','nodo');
            
            require_once(APPLICATION_PATH . '/models/Nodo.php');
            
        }
        
        public function indexAction()
        {
           $nodo = new Nodo();
           $return = $nodo->getNodo();
           
           $three = new Zend_Session_Namespace('three');
           $three->namesArray = $nodo->getNodosNamesArray();
           
           if(is_array($return))
           {
               foreach($return as $val)
               {
                   echo $val;
                   echo "<br>";
               }
           }
        }
        
        
        public function createAction()
        {
            $registry = Zend_Registry::getInstance();
            $registry->set('tela','criarNodo');
            
            require_once APPLICATION_PATH . '/forms/Forms_CriarNodo.php';
            $form = new Forms_CriarNodo();
            $form->setAction($this->_helper->url('recivecreatenod'));
            $form->showForm('create','/nodo/reciveform');
        }
        
    
        
        public function intoAction()
        {
            
            $registry = Zend_Registry::getInstance();
            $registry->set('tela','intoNodo');
            
            $currentNod = new Zend_Session_Namespace('currentNod');
            
            if(isset($_POST['name']))
            {               
                $currentNod->name = $_POST['name'];
                $currentNod->aproved = $_POST['aproved'];
                $currentNod->nodsFilhos = $_POST['nodsFilhos'];
                $currentNod->nodsPais = $_POST['nodsPais'];
            
            }
            $this->view->title = $currentNod->name; 
            
            require_once  APPLICATION_PATH . "/models/Artigo.php";
            
            $artigo = new Artigo();
            $artigo->showArticles($currentNod->name);
            
                     
        }
        
        public function editAction()
        {
            $registry = Zend_Registry::getInstance();
            $registry->set('tela','editNodo');
            
            require_once APPLICATION_PATH . '/forms/Forms_EditarNodo.php';
            
            $currentNod = new Zend_Session_Namespace('currentNod');
                  
            $form = new Forms_EditarNodo();
            $form->setAction($this->_helper->url('recivehiddenform'));
            $form->showForm('edit','/nodo/reciveform',
                            $currentNod->name,$currentNod->nodsPais,$currentNod->nodsFilhos);
            
        }
        
       
        public function reciveformAction()
        {                
            $currentNod = new Zend_Session_Namespace('currentNod'); 
            $three = new Zend_Session_Namespace('three');
            $nodo = new Nodo();
            
            $action = $_POST['action'] != "" ? $_POST['action'] : $_GET['action'];
            
            switch($action)
            {
                  case 'delete':                      
                    $nodo = new Nodo();
                    $nodo->removeNodo($currentNod->name);
                    $this->_redirect('/nodo/index');
                    break;
                
                  case 'edit':       
                    $nodosPais = $_POST['nameNodoPai'];
                    $nodosFilhos = $_POST['nameNodoFilho'];
                    $oldNodsFather = $currentNod->nodsPais;
                    $oldNodsSon = $currentNod->nodsFilhos;                   
                     
                    $nodosPais = explode(',',$nodosPais);                
                    $nodosFilhos = explode(',',$nodosFilhos);
                    $oldNodsFather = explode(',',$oldNodsFather);
                    $oldNodsSon = explode(',',$oldNodsSon);
                                                       
                    if(!$nodo->verifyFatherNodos($three->namesArray,$nodosPais))
                    {
                        echo "Algum nodo pai nao existe no BD.";
                        break;
                    }                    
                    $nodo->editNodo($_POST['name'],
                                    $nodosPais,
                                    $nodosFilhos,
                                    $_POST['aprovation'],
                                    $currentNod->name,
                                    $oldNodsFather,
                                    $oldNodsSon
                                  );                    
                    $currentNod->name = $_POST['name'];
                    $currentNod->nodsPais = $_POST['nameNodoPai'];
                    $currentNod->nodsFilhos = $_POST['nameNodoFilho'];
                    $currentNod->aproved = $_POST['aprovation'];
                    $this->_redirect('/nodo/into');
                    break;
                    
                  case 'create':                      
                    require_once APPLICATION_PATH . '/forms/Forms_CriarNodo.php';
                    $form = new Forms_CriarNodo();
                    $data = $this->_request->getPost();  
                    
                    $fathersArray = explode(',',$data['nameNodoPai']);
                   
                    if(!$nodo->verifyFatherNodos($three->namesArray,$fathersArray))
                    {
                        $this->view->message = "Algum nodo pai nao existe no BD.";                        
                        break;
                    }
                      
                    if($form->isValid($data))
                    {
                        $this->view->message = 'Sua alteracao ira para a aprovacao de algum moderador
                                                <br> ate sua aprovacao voce esta livre para alterar esse nodo <br><br>';
                        $nodo =  new Nodo();
                        $nodo->cadastrar($data);

                    }
                    else
                    {
                         $this->view->message = 'Dados Invalidos';
                    }
                      $this->view->message.= "<br><a href='/nodo/into'>Voltar</a>";
                      break;
                                          
            }           
             
        }
        
    }
?>
